package Core 
{
	import com.junkbyte.console.Cc;
	import Common.Constants;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import Fonts.CalibriEmbed;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class ImageDescriptor extends Sprite 
	{	
		static public const TEXT_UPDATED:String = 'text_updated';
		
		private const DEFAULT_TEXT:String = Constants.DESCRIPTION_CAPTION_DEFAULT;
		
		private var FLabel:TextField;
		private var FFormat:TextFormat;
		private var FImage:ImageContainer;
		private var FWidth:Number;
		private var FHeight:Number;
		
		public function ImageDescriptor(AImage:ImageContainer, AWidth:Number, AHeight:Number) 
		{
			FImage = AImage;			
			FWidth = AWidth;
			FHeight = AHeight;
			Update();
			FFormat = new TextFormat();			
			FFormat.font = new CalibriEmbed().fontName;
			FLabel = new TextField();			
			FLabel.autoSize = TextFieldAutoSize.NONE;			
			FLabel.type = TextFieldType.INPUT;
			FLabel.defaultTextFormat = FFormat;			
			FLabel.textColor = Constants.DESCRIPTION_TEXT_COLOR_DEFAULT;
			FLabel.addEventListener(MouseEvent.MOUSE_DOWN, OnTextClick);
			FLabel.addEventListener(KeyboardEvent.KEY_DOWN, OnChange);
			FLabel.addEventListener(FocusEvent.FOCUS_OUT, OnFocusOut);			
			FLabel.restrict = Constants.INPUT_RESTRICT;
			FLabel.multiline = true;
			FLabel.wordWrap = true;
			FLabel.width = AWidth - 4;
			FLabel.height = AHeight - 4;
			FLabel.x = 2;
			FLabel.y = 2;			
			FLabel.text = DEFAULT_TEXT;
			FLabel.maxChars = 128;
			focusRect = false;
			addChild(FLabel);			
		}
		
		public function Update():void 
		{
			var vColor:uint = FImage.IsLocal ? Constants.DESCRIPTION_BACKGROUND_LOCAL : Constants.DESCRIPTION_BACKGROUND;
			graphics.clear();
			graphics.beginFill(vColor, Constants.DESCRIPTION_ALPHA);
			graphics.drawRect(0, 0, FWidth, FHeight);
			graphics.endFill();
		}
		
		private function OnChange(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				stage.focus = null;
			}
		}
		
		private function OnFocusOut(e:FocusEvent):void 
		{
			if (FLabel.text == '')
			{
				FLabel.text = DEFAULT_TEXT;
				FLabel.textColor = Constants.DESCRIPTION_TEXT_COLOR_DEFAULT;
			}
			else
				dispatchEvent(new Event(TEXT_UPDATED));
		}
		
		private function OnTextClick(e:MouseEvent):void 
		{
			if(FLabel.text == DEFAULT_TEXT)
				FLabel.text = '';
			FLabel.textColor = Constants.DESCRIPTION_TEXT_COLOR;
		}
		
		public function set Text(AText:String)
		{
			if (!AText || AText == '')
			{
				FLabel.text = DEFAULT_TEXT;
				FLabel.textColor = Constants.DESCRIPTION_TEXT_COLOR_DEFAULT;
			}
			else
			{
				FLabel.text = AText;
				FLabel.textColor = Constants.DESCRIPTION_TEXT_COLOR;
			}
		}
		
		public function get Text():String
		{
			var vResult:String = '';
			if ((FLabel.text != Constants.DESCRIPTION_CAPTION_DEFAULT) && (FLabel.text != ''))
				vResult = FLabel.text;
			return vResult;
		}
	}
}